package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "菜品管理")
@Slf4j
@RestController
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    DishService dishService;

    @CacheEvict(cacheNames = "dish",key = "#dto.categoryId")
    @ApiOperation("新增菜品")
    @PostMapping
    public Result addDish(@RequestBody DishDTO dto){
        log.info("新增菜品 {}",dto);
        dishService.addDish(dto);
        return Result.success();
    }

    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dto){
        log.info("分页查询菜品 {}",dto);
        return Result.success(dishService.page(dto));
    }

    @ApiOperation("查询全部菜品")
    @GetMapping("/list")
    public Result list(DishPageQueryDTO dto){
        log.info("查询全部菜品 {}",dto);
        return Result.success(dishService.list(dto));
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "dish",allEntries = true)
    @DeleteMapping
    public Result deleteDish(@RequestParam List<Long> ids){
        log.info("删除菜品 {}",ids);
        dishService.delete(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        log.info("根据id查询菜品 {}",id);
        DishVO item = dishService.selectById(id);
        return Result.success(item);
    }

    /**
     * 修改菜品
     * @param dto
     * @return
     */
    @CacheEvict(cacheNames = "dish",allEntries = true)
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dto){
        log.info("修改菜品 {}",dto);
        dishService.updateDish(dto);
        return Result.success();
    }

}
